package com.yunduansing.demo.web.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yunduansing.demo.entity.MenuEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.model.common.ResponseModel;
import com.yunduansing.demo.service.MenuService;
import com.yunduansing.demo.web.BaseController;
import com.yunduansing.demo.model.menu.AntdMenuTreeModel;
import com.yunduansing.demo.model.menu.MenuTreeModel;
import com.yunduansing.demo.model.menu.SetStatusModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "菜单相关")
@RestController
public class MenuController extends BaseController {
    @Autowired
    private MenuService service;

    @GetMapping("/menu/tree")
    @ApiOperation("获取菜单树")
    public ResponseModel<MenuTreeModel> getTree(){
        return ok(service.getTree());
    }

    @GetMapping("/menu/user/tree")
    @ApiOperation("根据用户获取菜单树,如果userId=0，则会获取当前登录用户的id")
    public ResponseModel<AntdMenuTreeModel> getUserMenuTree(int userId){
        boolean admin=false;
        if(userId==0) {
            userId=this.getPassport().getUserId();
            admin=this.getPassport().isAdmin();
        }
        return ok(service.getUserMenuTree(userId,admin));
    }

    @PostMapping("/menu/create")
    @ApiOperation("创建菜单")
    public ResponseModel create(@RequestBody MenuEntity entity) throws RecordExistsException {
        var wrapper=new QueryWrapper<MenuEntity>();
        Map<String,Object> params=new HashMap<>();
        params.put("pid", entity.getPid());
        params.put("name", entity.getName());
        wrapper.allEq(params);
        var exist=service.list(wrapper);
        if(exist.size()>0) throw new RecordExistsException("相同父级下已存在名为："+entity.getName()+"的菜单。");
        return this.convertToResponseModel(service.save(entity), "创建失败。");
    }

    @PostMapping("/menu/update")
    @ApiOperation("修改菜单")
    public ResponseModel update(@RequestBody MenuEntity entity) throws RecordExistsException {
        var wrapper=new QueryWrapper<MenuEntity>();
        wrapper.eq("pid", entity.getPid()).eq("name", entity.getName()).ne("id", entity.getId());
        var exist=service.list(wrapper);
        if(exist.size()>0) throw new RecordExistsException("相同父级下已存在名为："+entity.getName()+"的菜单。");
        return this.convertToResponseModel(service.updateById(entity), "修改失败。");
    }

    @PostMapping("/menu/delete")
    @ApiOperation("删除菜单")
    public ResponseModel delete(@RequestBody List<Integer> ids){
        return convertToResponseModel(service.removeByIds(ids), "删除失败。");
    }

    @PostMapping("/menu/status")
    @ApiOperation("设置状态")
    public ResponseModel setStatus(@RequestBody SetStatusModel model) {
        var wrapper=new UpdateWrapper<MenuEntity>();
        wrapper.set("status", model.getStatus());
        wrapper.in("id", model.getIds());
        return convertToResponseModel(service.update(wrapper), "设置失败");
    }
    
}